package com.cash.loan.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.cash.loan.config.properties.ApiDruidProperties;
import com.github.pagehelper.PageHelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Properties;

/**
 * @author Administrator
 * @decription
 * @Date 2017-11-28 9:28
 */
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.cash.loan.modular.*.dao"})
public class MybatisPlusConfig {

    @Autowired
    private ApiDruidProperties druidProperties;

    private DruidDataSource druiddataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }

    @Bean
    public DruidDataSource dataSource() {
        return druiddataSource();
    }

    /**
     * pageHelper分页插件
     * @return
     */
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        //添加配置，也可以指定文件路径
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        //启用合理化时，如果pageNum<1会查询第一页，如果pageNum>pages会查询最后一页
        properties.setProperty("reasonable", "false");
        properties.setProperty("dialect","mysql");
        pageHelper.setProperties(properties);
        return pageHelper;
    }

}
